import { TSTRouteObject } from '../fc-router-dom/fc-route';

export type RouteUrlMap = {
  [key: string]: string | undefined;
};

export const convertTSTRoutesToURLMap = (routes: TSTRouteObject[]): RouteUrlMap => {
  const map: RouteUrlMap = {};
  const loop = (nodes: TSTRouteObject[], parentUrl?: string) => {
    nodes
      .filter(({ key }: any) => !!key)
      .forEach(({ key, path, children }: any) => {
        const url: string | undefined = ((parentUrl ?? '') + (path?.startsWith('/') ? path : `/${path}`)).replace(
          '//',
          '/',
        );

        map[key] = url;
        children && loop(children, url);
      });
  };

  loop(routes);

  return map;
};
